
require("llcc")

local version = _VERSION;

local lcc = {};
local callback_userdata = LCC_get_callback_userdata(
function(sig,kind,str)
    --print(sig,kind,str);
end
);
local defaultCallBackUserData = callback_userdata;
local defaultCallBackFunc = LCC_get_callback_func();
local lcc = {};
lcc.src = {};

function lcc.defaultCallBack(func)
    defaultCallBackUserData = LCC_get_callback_userdata(func);
    defaultCallBackFunc = LCC_get_callback_func();
end

function lcc.codeCompleteAt(path,code,line,col,match_word,callback)
    local context = lcc.src[path];
    if(context==nil)then
        context = LCC_init();

        LCC_add_argv(context,"-x");
        LCC_add_argv(context,"c++");

        local function cxx_flags_add(v)
            LCC_add_argv(context,v);
        end

        LCC_set_file(context,path);
        LCC_add_argv(context,path);

        cxx_flags_add"-isystemK:/ProgramFiles/msys2/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.1.0/../../../../include/c++/6.1.0"
        cxx_flags_add"-isystemK:/ProgramFiles/msys2/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.1.0/../../../../include/c++/6.1.0/i686-w64-mingw32"
        cxx_flags_add"-isystemK:/ProgramFiles/msys2/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.1.0/../../../../include/c++/6.1.0/backward"
        cxx_flags_add"-isystemK:/ProgramFiles/msys2/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.1.0/include"
        cxx_flags_add"-isystemK:/ProgramFiles/msys2/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.1.0/../../../../include"
        cxx_flags_add"-isystemK:/ProgramFiles/msys2/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.1.0/include-fixed"
        cxx_flags_add"-isystemK:/ProgramFiles/msys2/mingw32/bin/../lib/gcc/i686-w64-mingw32/6.1.0/../../../../i686-w64-mingw32/include"

        LCC_set_tuopt(context,0x3f);

        LCC_init_clang(context);
        lcc.src[path] = context;
    end
    if(code)then
        LCC_buffer_add(context,path,code);
    end
    if(match_word)then
        LCC_set_match(context,match_word);
    else
        LCC_set_match(context,"");
    end
    if(callback)then
        LCC_completion_proc_callback(context,path,line,col,LCC_get_callback_userdata(callback),defaultCallBackFunc);
    else
        LCC_completion_proc_callback(context,path,line,col,defaultCallBackUserData,defaultCallBackFunc);
    end
end

local function readText(fn)
    return io.open(fn,"rb"):read('*a');
end

if(test or #arg>1)then
    lcc.codeCompleteAt('a.c',readText('a.c'),7,10,nil,function() end);
    lcc.codeCompleteAt('a.c',readText('b.c'),7,10);
end


return lcc;

